package com.xy.p2p.action.front;



import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.p2p.dao.TfinanceinfoDao;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Tmemberassetaccountdetails;
import com.xy.p2p.model.Tmembercredit;
import com.xy.p2p.model.Ttenderrecord;
import com.xy.p2p.service.TfinanceinfoService;
import com.xy.p2p.service.TmemberService;
import com.xy.p2p.service.TmemberassetaccountdetailsService;



@Controller
public class TmemberassetaccountdetailsAction{
	private static final Logger log = LoggerFactory.getLogger(TmemberassetaccountdetailsAction.class);
	public TmemberassetaccountdetailsAction() {
	}
 
 
 	/**
	 * 会员资产账户交易明细表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberassetaccountdetailsAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tmemberassetaccountdetails", new Tmemberassetaccountdetails());
        setDict(model);
		return "tmemberassetaccountdetailsEdit";
	}

	/**
	 * 会员资产账户交易明细表新增保存
	 * 
	 * @param tmemberassetaccountdetails
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tmemberassetaccountdetailsSave.do")
	public String save(Tmemberassetaccountdetails tmemberassetaccountdetails, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tmemberassetaccountdetails, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tmemberassetaccountdetails = tmemberassetaccountdetailsService.save(tmemberassetaccountdetails);
		return "redirect:v_tmemberassetaccountdetailsList.do";
	}

	/**
	 * 会员资产账户交易明细表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tmemberassetaccountdetailsDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tmemberassetaccountdetailsService.remove(id);
			}
		}
		return "redirect:v_tmemberassetaccountdetailsList.do";
	}

	/**
	 * 会员资产账户交易明细表编辑页面
	 * 
	 * @param maadid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberassetaccountdetailsEdit.do")
	public String edit(long maadid, HttpServletRequest request, ModelMap model) {
		Tmemberassetaccountdetails tmemberassetaccountdetails = tmemberassetaccountdetailsService.get(maadid);
		model.addAttribute("tmemberassetaccountdetails", tmemberassetaccountdetails);
        setDict(model);
		return "tmemberassetaccountdetailsEdit";
	}

	/**
	 * 会员资产账户交易明细表详情
	 * 
	 * @param maadid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberassetaccountdetailsDetail.do")
	public String detail(long maadid, HttpServletRequest request, ModelMap model) {
		Tmemberassetaccountdetails tmemberassetaccountdetails = tmemberassetaccountdetailsService.get(maadid);
		model.addAttribute("tmemberassetaccountdetails", tmemberassetaccountdetails);
        setDict(model);
		return "tmemberassetaccountdetailsView";
	}

	/**
	 * 会员资产账户交易明细表查询（分页）
	 * 
	 * @param tmemberassetaccountdetails
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberassetaccountdetailsList.do")
	public String listPage(Tmemberassetaccountdetails tmemberassetaccountdetails, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tmemberassetaccountdetailsService.getTmemberassetaccountdetailsByPage(toMap(tmemberassetaccountdetails),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tmemberassetaccountdetailsList";
	}

	/**
	 * 会员资产账户交易明细表查询
	 * 
	 * @param tmemberassetaccountdetails
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("member/v_tmemberassetaccountdetailsList.jspx")
	public String list(Tmemberassetaccountdetails tmemberassetaccountdetails, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
//		tmemberassetaccountdetails = new Tmemberassetaccountdetails();
		tmemberassetaccountdetails.setTmember((Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY));
		Pagination pagination = tmemberassetaccountdetailsService.getTmemberassetaccountdetailsByPage(toMap(tmemberassetaccountdetails),
				pageNo, CookieUtils.getPageSize(request));
//		List<Tmemberassetaccountdetails> list = tmemberassetaccountdetailsService.getTmemberassetaccountdetails(toMap(tmemberassetaccountdetails));
//		List<Tmemberassetaccountdetails> list = tmemberassetaccountdetailsService.getAll();
//		Collections.sort(list);
//		int pageSize = 20;
//		Pagination pagination =new Pagination(pageNo, pageSize, list.size());
//		if(pageNo*pageSize>list.size()){
//			pagination.setList(list.subList((pageNo-1)*pageSize , list.size()));
//		}else{
//			pagination.setList(list.subList((pageNo-1)*pageSize , (pageNo*pageSize)));
//		}
		for (Object t : pagination.getList()) {
			if(this.tryPaseLong(((Tmemberassetaccountdetails)t).getOtherside())){
				((Tmemberassetaccountdetails)t).setTfinanceinfo(tfinanceinfoService.get(Long.parseLong(((Tmemberassetaccountdetails)t).getOtherside())));
			}
		}
//		Collections.sort((List<Tmemberassetaccountdetails>) pagination.getList());
		model.addAttribute("pagination", pagination);
//		model.addAttribute("list", pagination);
//		pagination.getList().
        setDict(model);
        //用于导航栏
        model.addAttribute("dis", 3);
		return "/front/member/dealDetail.html";
	}

	private boolean tryPaseLong(String prarm){
		try {
			Long.parseLong(prarm);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	
	
//	public static void main(String[] args) {
//		TmemberassetaccountdetailsAction t = new TmemberassetaccountdetailsAction();
//		System.out.println(t.tryPaseInt("3"));
//		
//	}
	 

 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tmemberassetaccountdetails
	 * @return
	 */	private Map<String,Object> toMap(Tmemberassetaccountdetails tmemberassetaccountdetails){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tmemberassetaccountdetails.getMaadid()!=null&&!tmemberassetaccountdetails.getMaadid().equals("")){
			map.put("maadid", tmemberassetaccountdetails.getMaadid());
		}
		if(tmemberassetaccountdetails.getTmember()!=null){
			map.put("Tmember", tmemberassetaccountdetails.getTmember());
		}
		if(tmemberassetaccountdetails.getTranamount()!=null&&!tmemberassetaccountdetails.getTranamount().equals("")){
			map.put("tranamount", tmemberassetaccountdetails.getTranamount());
		}
		if(tmemberassetaccountdetails.getBorrowing()!=null&&!tmemberassetaccountdetails.getBorrowing().equals("")){
			map.put("borrowing", tmemberassetaccountdetails.getBorrowing());
		}
		if(tmemberassetaccountdetails.getTrantype()!=null&&!tmemberassetaccountdetails.getTrantype().equals("")){
			map.put("trantype", tmemberassetaccountdetails.getTrantype());
		}
		if(tmemberassetaccountdetails.getTrandatetime()!=null&&!tmemberassetaccountdetails.getTrandatetime().equals("")){
			map.put("trandatetime", tmemberassetaccountdetails.getTrandatetime());
		}
		if(tmemberassetaccountdetails.getOtherside()!=null&&!tmemberassetaccountdetails.getOtherside().equals("")){
			map.put("otherside", tmemberassetaccountdetails.getOtherside());
		}
		if(tmemberassetaccountdetails.getRemark()!=null&&!tmemberassetaccountdetails.getRemark().equals("")){
			map.put("remark", tmemberassetaccountdetails.getRemark());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("borrowingMap", DictUtils.getDictByType(DictUtils.TMEMBERASSETACCOUNTDETAILS_BORROWING));
      model.addAttribute("trantypeMap", DictUtils.getDictByType(DictUtils.TMEMBERASSETACCOUNTDETAILS_TRANTYPE));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tmemberassetaccountdetails tmemberassetaccountdetails, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TmemberassetaccountdetailsService tmemberassetaccountdetailsService;   
	@Resource
	private TfinanceinfoService tfinanceinfoService;
	@Resource
	private TmemberService tmemberService;
}